package com.telkomsigma.hibernate.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.telkomsigma.hibernate.persistence.captain.entity.Captain;
import com.telkomsigma.hibernate.persistence.player.entity.Player;
import com.telkomsigma.hibernate.persistence.team.entity.Team;
import com.telkomsigma.hibernate.services.captain.CaptainService;
import com.telkomsigma.hibernate.services.player.PlayerService;
import com.telkomsigma.hibernate.services.team.TeamService;

@Controller
@SessionAttributes
public class ControllerCaptain {
	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	CaptainService captainService = (CaptainService)context.getBean("captainService");
	TeamService teamService = (TeamService)context.getBean("teamService");
	PlayerService playerService = (PlayerService)context.getBean("playerService");
	
	@RequestMapping(value = "/captain", method = RequestMethod.POST)
	public ModelAndView saveCaptain(@RequestParam("playerId") String playerId, @RequestParam("teamId") String teamId){		
		Captain captain = new Captain();
		captain.setPlayer_id(playerId);			
		captain.setTeam_id(teamId);
		
		String location = "captain.htm?teamId=" + teamId;
		captainService.insert(captain);
		Map<String, Object> captainQuery = new HashMap<String, Object>();
		captainQuery.put("message", "Insert Captain Success");	
		captainQuery.put("location", location);
		return new ModelAndView("captain","captainQuery", captainQuery);		
	}
	
	@RequestMapping(value = "/captain", method = RequestMethod.GET)
	public ModelAndView listOfTeam(@RequestParam("teamId") String teamId){
		Team teamName = teamService.findById(teamId);
				
		Captain captain = new Captain();
		captain.setTeam_id(teamId);
		
		String playerId = null;
		List<Captain> captainList = captainService.findAll(0);
		List<Player> captainWithPlayer = new ArrayList<Player>();
		for (Captain capt : captainList){
			playerId = capt.getPlayer_id();
			Player player = playerService.findById(playerId);			
			player.getFirstName();
			player.getLastName();
			player.getPlayerId();
			captainWithPlayer.add(player);			
		}
				
		Map<String, Object> captainMap = new HashMap<String, Object>();
		captainMap.put("listCaptain", captainWithPlayer);
		captainMap.put("teamNameCaptain", teamName.getName());
		captainMap.put("teamIdCaptain", teamName.getId());
		captainMap.put("module","list");
        return new ModelAndView("captain","captainMap", captainMap);		
	}
	
	@RequestMapping(value = "/captainDelete", method = RequestMethod.GET)
	public ModelAndView deleteCaptain(@RequestParam("playerId") String playerId, @RequestParam("teamId") String teamId){
		Captain captain = new Captain();		
		captain.setPlayer_id(playerId);
		boolean rowCount = captainService.remove(captain);
		Map<String, Object> captainQuery = new HashMap<String, Object>();
		String location = "captain.htm?teamId=" + teamId;
		if (rowCount == true){
			captainQuery.put("message", "Delete Success");
		}else{
			captainQuery.put("message", "Delete Fail");
		}
		captainQuery.put("location", location);
		return new ModelAndView("captain","captainQuery", captainQuery);		
	}
	
	@RequestMapping(value = "/captainAdd", method = RequestMethod.GET)
	public ModelAndView formCaptain(@RequestParam("teamId") String teamId){
		List<Player> playerList = playerService.findByProperty("teamId", teamId, 0);
		Map<String, Object> captainMap = new HashMap<String, Object>();
		captainMap.put("playerList", playerList);
		captainMap.put("teamId", teamId);
		captainMap.put("module", "add");
		return new ModelAndView("captain","captainMap", captainMap);
	}
}
